<template>
	<a-upload
	    multiple="multiple"
	    :action="uploadAddress"
	    :headers='headers'
	    @change="handleChangeFile"
	    :defaultFileList="defaultFileList"
	    :fileList='fileList'  
	    class='text-left'
	    :remove='removeFile'

	    
	    :showUploadList='showUploadList' 
	  >
	    <a-button> <a-icon type="upload" />上传文件</a-button><br>
	    <sub  class='mb28'  v-if='uptip'>({{uptip}})</sub>
	</a-upload>

</template>

<script >
import { download_file,uploadFile }  from '@/api/common'
 	export default {
		name:'fileUpload',
		props:{
			uploadName:{
				type:String,
				default:''
			},
			multiple:{
				type:Boolean,
				default:false
			},
			defaultFileList:{
				type:[Object,Array],
				default:()=>[]
			},
			fileList:{
				type:[Object,Array],
				default:()=>[]
			},
			//默认上传提示文字
			uptip:{
				type:String,
				default:''
			},
			//当前上传仅支持单文件上传
			onefile:{
				type:Boolean,
				default:false
			},
			//fileList 显示图标&删除按钮 setting
			showUploadList:{
				type:Object,
				default:()=>({
					showPreviewIcon: true, showRemoveIcon: true 
				})
			}
		},
		data(){
			return {
				uploadAddress:process.env.BASE_API+'/op/fileupload/upload_file', //
				headers:{
					'X-Token':sessionStorage.getItem('token')
				},
				// defaultFileList: [
		  //         // {
		  //         //   uid: '1',
		  //         //   name: 'xxx.png',
		  //         //   status: 'done',
		  //         //   response: 'Server Error 500', // custom error message to show
		  //         // }, 
		  //       ],
			}
		},
		created(){ 

		},
		methods:{
			/**/
			customRequest(options){
				console.log(options)
				  const formData = new FormData() 

		          formData.append('file',options.file)

		          uploadFile(formData).then((res)=>{
		          		console.log(res)
		          })

			},
			/*upload*/
		    handleChangeFile(info) {
		    	// console.log(info)
		    	if(this.onefile){
		    		if(info.fileList.length>1){ 
			    		const h = this.$createElement;
				        this.$info({
				          title: '提示',
				          content: h('div', {}, [
				            h('p', '合同文件只允许上传一个文件，请确认删除后重试'),
				            // h('p', 'some messages...some messages...'),
				          ]),
				          okText:'知道了',
				          onOk() {},
				        });
			    		return false
			    	}
		    	}
		    	
				if (info.file.status == 'uploading') {
					// console.log(info.file, info.fileList); 
					this.$emit('uploading',info)
				}
				if (info.file.status === 'done') {
					this.$message.success(`${info.file.name} 文件上传成功`); 
					this.$emit('done',info.fileList)
				} else if (info.file.status === 'error') {
					this.$message.error(`${info.file.name} file upload failed.`);
				}
		    },
		    // @preview='preview'
		    preview(file){
		       download_file(file.code)
		    },
		    removeFile(file){
		    	// console.log(file.response.data.code || file.code)
		    	let code = file.response.data.code || file.code
		    	this.$emit('removeFile',code)
		    }
		}
	}
</script>